# Copyright 2015 the original author or authors. All rights reserved.
# site: http://www.ganshane.com
IF(MONAD_HAVE_LINUX)
  SET(CMAKE_CXX_FLAGS "-fPIC -fno-omit-frame-pointer -Wall -Wextra -fno-strict-aliasing ${CMAKE_CXX_FLAGS}")
ENDIF()
IF(MONAD_HAVE_WINDOWS)
  IF(MINGW)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L/build/${ARCH} ")
    IF(ARCH MATCHES "w32")
      SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32 ")
    ELSE()
      SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64 -D_AMD64_ ")
    ENDIF()
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/build/mingw -static -DCURL_STATICLIB -DHAVE_LIBZ -DHAVE_ZLIB_H ")
  ENDIF()
ENDIF()



FILE(GLOB_RECURSE ROOT_SOURCES ${PROJECT_SOURCE_DIR}/src/*)
ADD_LIBRARY(monad STATIC ${ROOT_SOURCES})


##################### JNI BRIDGE ##############
# swig support
FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})
INCLUDE(UseSWIG)

# java jni support
FIND_PACKAGE(Java REQUIRED)
FIND_PACKAGE(JNI REQUIRED)
INCLUDE_DIRECTORIES(${JAVA_INCLUDE_PATH})
INCLUDE_DIRECTORIES(${JAVA_INCLUDE_PATH2})

# create swig ouput directory
SET(CMAKE_SWIG_OUTDIR javaapi)
ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_SWIG_OUTDIR}
  COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_SWIG_OUTDIR}
  COMMAND ${CMAKE_COMMAND} -E make_directory javaapi
  COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/src
  COMMENT "Creating target directory for monad project JNI sources")
ADD_CUSTOM_TARGET(jni-swig-out-dir ALL DEPENDS ${CMAKE_SWIG_OUTDIR})

SET(SWIG_I monad.i)
SET_SOURCE_FILES_PROPERTIES(${SWIG_I} PROPERTIES CPLUSPLUS ON)
SET_SOURCE_FILES_PROPERTIES(${SWIG_I} PROPERTIES SWIG_FLAGS "-package;monad.jni.services.gen")
SET(SWIG_SOURCES ${SWIG_I})
MESSAGE(STATUS "SWIG SOURCES for ${SWIG_SOURCES}")
SWIG_ADD_MODULE(monad4j JAVA ${SWIG_SOURCES})
SWIG_LINK_LIBRARIES(monad4j monad)
SET_TARGET_PROPERTIES(monad4j PROPERTIES LINKER_LANGUAGE CXX)
ADD_DEPENDENCIES(monad4j monad jni-swig-out-dir)

IF(MONAD_HAVE_ROCKSDB)
  TARGET_LINK_LIBRARIES(monad4j rocksdb)
ELSE()
  TARGET_LINK_LIBRARIES(monad4j leveldb)
ENDIF()

IF(ENABLE_WIREDTIGER)
  TARGET_LINK_LIBRARIES(monad4j rt)
ENDIF()

IF(NOT MONAD_HAVE_WINDOWS)
  TARGET_LINK_LIBRARIES(monad4j snappy pthread)
ENDIF()

SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -fno-strict-aliasing ${CMAKE_CXX_FLAGS} ")
SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG")

IF(MONAD_HAVE_WINDOWS)
  IF(MINGW)
    SET_TARGET_PROPERTIES(monad4j PROPERTIES LINK_FLAGS "-static -static-libgcc -static-libstdc++ -fno-strict-aliasing ")
  ENDIF()
  TARGET_LINK_LIBRARIES(monad4j shlwapi) #wldap32 ws2_32 version)
ENDIF()

